package com.qf.service.impl;

import com.alibaba.druid.support.spring.stat.annotation.Stat;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsCategoryService;
import com.qf.vo.CatVo;
import com.qf.vo.CategorySellAmts;
import com.sun.org.apache.bcel.internal.generic.LADD;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DtsCategoryServiceImpl implements DtsCategoryService {
    @Autowired
    StatMapper statMapper;
    @Autowired
    DtsCategoryMapper categoryMapper;


    @Override
    public List<CategorySellAmts> findCategorySellDate() {
        List<CategorySellAmts> list = statMapper.findCategorySellDate();
        return list;
    }

    @Override
    public IPage<DtsCategory> findByPage(Integer page, Integer limit, String sort, String order, Integer id, String name) {
        IPage<DtsCategory> iPage = new Page<>(page,limit);
        QueryWrapper<DtsCategory> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(id)){
            qw.eq("id",id);
        }
        if (!StringUtils.isEmpty(name)){
            qw.like("name",name);
        }
        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            if(order.equalsIgnoreCase("desc")) {
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }
        categoryMapper.selectPage(iPage,qw);
        return iPage;
    }

    @Override
    public void delete(DtsCategory category) {
        categoryMapper.deleteById(category);
    }

    @Override
    public List<CatVo> findGoodsClass() {
        LambdaQueryWrapper<DtsCategory> qw1 = new LambdaQueryWrapper<>();
        qw1.eq(DtsCategory::getPid,0);
        qw1.eq(DtsCategory::getDeleted,false);
        List<DtsCategory> catLv1 = categoryMapper.selectList(qw1);//获得一级分类
        List<CatVo> catVos = catLv1.stream().map(cat -> {
            CatVo catVo = new CatVo();
            catVo.setLabel(cat.getName());
            catVo.setValue(cat.getId());


            LambdaQueryWrapper<DtsCategory> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(DtsCategory::getPid, cat.getId());
            qw2.eq(DtsCategory::getDeleted, false);
            List<DtsCategory> catLv2 = categoryMapper.selectList(qw2);
            List<CatVo> collect = catLv2.stream().map(cat2 -> {

                CatVo catVo1 = new CatVo();
                catVo1.setValue(cat2.getId());
                catVo1.setLabel(cat2.getName());
                return catVo1;
            }).collect(Collectors.toList());
            catVo.setChildren(collect);
            return catVo;
        }).collect(Collectors.toList());

        return catVos;
    }
}
